package cn.tedu.medicalappointmentsystemk.dao.persist.repository.impl;

import cn.tedu.medicalappointmentsystemk.dao.persist.mapper.UserMapper;
import cn.tedu.medicalappointmentsystemk.dao.persist.repository.UserRepository;
import cn.tedu.medicalappointmentsystemk.pojo.bo.UserSelectBO;
import cn.tedu.medicalappointmentsystemk.pojo.po.UserPO;
import cn.tedu.medicalappointmentsystemk.pojo.vo.UserLoginVO;
import cn.tedu.medicalappointmentsystemk.pojo.vo.UserSelectVO;
import cn.tedu.medicalappointmentsystemk.pojo.vo.UserUpdateVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;

import java.io.File;
import java.util.List;

@Slf4j
@Repository
public class UserRepositoryImpl implements UserRepository {
    @Autowired
    private UserMapper userMapper;

    @Value("${file-path}")
    private String dirPath;

    @Override
    public Integer addNew(UserPO po) {
        return userMapper.insert(po);
    }

    @Override
    public UserLoginVO selectByUsername(String username) {
        return userMapper.selectByUsername(username);
    }

    @Override
    public UserPO selectById(Long id) {
        QueryWrapper<UserPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        return userMapper.selectOne(queryWrapper);
    }

    @Override
    public Integer updatePassword(UserPO po) {
        return userMapper.updateById(po);
    }

    @Override
    public UserPO selectByMobile(String mobile) {
        QueryWrapper<UserPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mobile",mobile);
        return userMapper.selectOne(queryWrapper);
    }

    @Override
    public Integer updateUser(UserUpdateVO vo) {
        return userMapper.update(vo);
    }

    @Override
    public UserSelectVO selectUserById(Long id) {
        return userMapper.selectUserById(id);
    }

    @Override
    public Integer deleteById(Long id) {
        UserPO po = userMapper.selectById(id);
        new File(dirPath+po.getImgUrl()).delete();
        return userMapper.deleteById(id);
    }

    @Override
    public List<UserSelectBO> select() {
        return userMapper.selectList();
    }

    @Override
    public Integer countByUsername(String username) {
        QueryWrapper<UserPO> qw = new QueryWrapper<>();
        qw.eq("username",username);
        return userMapper.selectCount(qw);
    }

    @Override
    public Integer countByMobile(String mobile) {
        QueryWrapper<UserPO> qw = new QueryWrapper<>();
        qw.eq("mobile",mobile);
        return userMapper.selectCount(qw);
    }

    @Override
    public Integer countByEmail(String email) {
        QueryWrapper<UserPO> qw = new QueryWrapper<>();
        qw.eq("email",email);
        return userMapper.selectCount(qw);
    }

    @Override
    public String selectNicknameById(Long id) {
        return userMapper.selectNicknameById(id);
    }

    @Override
    public Long selectIdByUsername(String username) {
        return userMapper.selectIdByUsername(username);
    }

}
